<template>
	<view class="auction">
		<view class="auction-box">
			<view class="auction-main">
				<view class="auction-main-chunk" v-if="auctionList.length>0">
					<view class="auction-main-chunk-header">
						<view class="_mian-chunk-header-title"><text>最近成交</text></view>
					</view>
					<view class="auction-main-chunk-content">
						<mosowe-text-scroll-row
							:list="auctionList"
							:actives="chooseList"
							textKey="name"
							delay="1000"
							initPosition="right"
							@change="chooseThis"
						/>
					</view>
					
				</view>
				<view class="auction-main-chunk">
					<view class="auction-main-chunk-header">
						<view class="_mian-chunk-header-title"><text>拍卖列表</text></view>
						<view class="_mian-chunk-header-option" @click="toMyAuction">
							<image class="_mian-chunk-header-option-icon" src="../../static/tab_bar/myCenter-icon.png" mode=""></image>
							<text>我的竞拍</text>
						</view>
					</view>
					<view class="auction-main-chunk-waterfall">
						<view class="auction-main-chunk-waterfall-view auction-main-chunk-waterfall-view-l"><helang-waterfall v-for="item,index in goodsListL" :key="index" :params="item" @click="getDetail"></helang-waterfall></view>
						<view class="auction-main-chunk-waterfall-view auction-main-chunk-waterfall-view-r"><helang-waterfall v-for="item,index in goodsListR" :key="index" :params="item" @click="getDetail"></helang-waterfall></view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageTitle:'拍卖列表',
				chooseList: [],
				auctionList: [],
				goodsList:[],
				goodsListL:[],
				goodsListR:[],
				total:0,
				list:[],
				params:{
					page: 0,
					pageSize: 10
				},
				pageNum:0,
				getListFlag:false,
			};
		},
		onLoad() {
			// this.getAuctionSoldList()
			this.getAuctionInfoList()
		},
		onPullDownRefresh() {
			this.params.page = 1;
			// this.getAuctionSoldList()
			this.getAuctionInfoList()
		},
		onReachBottom() {
			if (!this.getListFlag) return false
			if (this.params.page<this.pageNum) {
				this.params.page++;
				this.getAuctionInfoList()
			} else {
				uni.showToast({
					title:'没有更多数据了',
					icon:'none'
				})
			}
		},
		methods:{
			getAuctionInfoList() {
				this.$api.getAuctionInfoList(this.params).then(res => {
					console.log(res)
					this.getListFlag = true;
					uni.stopPullDownRefresh()
					if (res.code == 200) {
						if(this.params.page == 1) this.list = [], this.goodsListL = [], this.goodsListR = []
						// this.list = this.list.concat(res.data.records)
						this.initGoodsList(res.data.records)
						this.total = res.data.total
						let pageNum = (this.total / this.params.pageSize).toFixed(0);
						if (this.total % this.params.pageSize > 0) {
							this.pageNum = Number(pageNum) + 1;
						} else {
							this.pageNum = Number(pageNum);
						}
					}
				})
			},
			getAuctionSoldList() {
				this.$api.getAuctionSoldList().then(res => {
					console.log(res)
					if (res.code ==  200) {
						this.auctionList = res.data.auctionInfos;
					}
				})
			},
			// 为了更好的效果，尽量重复衔接json数据
			chooseThis (data) {
				// console.log(data);
				data = JSON.parse(data);
				if (this.chooseList.length > 0) { // 此处判断有其他简化的方法
					let hasOne = false;
					for (let i = 0; i < this.chooseList.length; i++) {
						let item = this.chooseList[i];
						if (Number(item.id) === Number(data.id)) {
							this.chooseList.splice(i, 1);
							hasOne = true;
							break;
						}
					}
					if (!hasOne) {
						this.chooseList.push(data);
					}
				} else {
					this.chooseList.push(data);
				}
	
				// console.log(this.chooseList);
			},
			initGoodsList(list) {
				let goodsListL = [],goodsListR = []
				goodsListL = list.filter((item,index) => {
					// console.log(item,index)
					let t = null
					if ((index+1) % 2 !== 0) {
						t = item;
					}
					return t;
				})
				goodsListR = list.filter((item,index) => {
					// console.log(item,index)
					let t = null
					if ((index+1) % 2 === 0) {
						t = item;
					}
					return t;
				})
				this.goodsListL = this.goodsListL.concat(goodsListL);
				this.goodsListR = this.goodsListR.concat(goodsListR);
				// console.log(this.goodsListL,this.goodsListR)
			},
			getDetail(e) {
				console.log(e)
				uni.navigateTo({
					url:'/pages/auctionDetail/auctionDetail?auctionId='+e.auctionId
				})
			},
			toMyAuction() {
				uni.navigateTo({
					url:'/pages/myAuction/myAuction'
				})
			}
		}
	}
</script>

<style lang="less">
	.auction{
		width: 100%;
		min-height: 100vh;
		background-color: #16181D;
		font-family: PingFangSC-Medium, PingFang SC, '微软雅黑';
		font:caption;
		display: flow-root;
		.auction-box{
			padding: 0 32rpx;
			box-sizing: border-box;
		}
		.auction-main{
			width: 100%;
			display: flex;
			flex-direction: column;
			.auction-main-chunk{
				width: 100%;
				margin-top: 48rpx;
				.auction-main-chunk-header{
					width: 100%;
					display: flex;
					flex-wrap: nowrap;
					justify-content: space-between;
					align-items: center;
					._mian-chunk-header-title{
						font-size: 36rpx;
						font-weight: 500;
						color: #FFFFFF;
					}
					._mian-chunk-header-option{
						display: flex;
						flex-wrap: nowrap;
						align-items: center;
						font-size: 28rpx;
						font-weight: 400;
						color: #FFFFFF;
						._mian-chunk-header-option-icon{
							width: 38rpx;
							height: 38rpx;
							min-width: 38rpx;
							margin-right: 10rpx;
						}
					}
				}
				.auction-main-chunk-content{
					margin-top: 20rpx;
				}
				.auction-main-chunk-waterfall{
					margin-top: 20rpx;
					display: flex;
					flex-direction: row;
					flex-wrap: nowrap;
					justify-content: flex-start;
					align-items: flex-start;
					align-content: flex-start;
					.auction-main-chunk-waterfall-view{
						width: 50%;
					}
					.auction-main-chunk-waterfall-view-r{
						display: flex;
						flex-direction: column;
						justify-content: flex-end;
						align-items: end;
					}
				}
			}
		}
	}
</style>
